package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.Arrays;

/**
 * createTime：2021/8/17 15:29
 * description：Summing3
 */
public class Summing3 {
    static long basicSum(Long[] ia) {
        long sum = 0;
        int size = ia.length;
        for (Long aLong : ia) {
            sum += aLong;
        }
        return sum;
    }

    public static final int SZ = 100_000_000;
    //注意这里的类型转换，一定的程度上防止了内存的溢出
    public static final long CHECK = (long) SZ * ((long) SZ + 1) / 2;

    public static void main(String[] args) {
        System.out.println(CHECK);
        Long[] longs = new Long[SZ + 1];
        Arrays.parallelSetAll(longs, o -> (long) o);
        Summing.timeTest("Long array stream reduce", CHECK, () -> Arrays.stream(longs).reduce(0L, Long::sum));
        Summing.timeTest("long basic sum", CHECK, () -> {
            Arrays.parallelPrefix(longs, Long::sum);
            return longs[longs.length - 1];
        });
    }
}
